﻿using System;
using System.Collections.Generic;
using BCEntity.OA.Attendance.AttendanceLocation;
using BCData.OA.Attendance.AttendanceLocation;
using BCDto.MySql.Admin;
using BCDto.OA.Attendance.AttendanceLocation;
using BCService.Utils;

namespace BCService.OA.Attendance.AttendanceLocation
{
    /// <summary>
    /// 打卡地址服务
    /// </summary>
    public class AttendanceLocationService : IAttendanceLocationService
    {
        private readonly IAttendanceLocationData attendanceLocationData;

        public AttendanceLocationService(IAttendanceLocationData attendanceLocationData)
        {
            this.attendanceLocationData = attendanceLocationData;

        }
        /// <summary>
        /// 新创建打卡地址信息
        /// </summary>
        /// <param name="admin">当前登录管理员</param>
        /// <param name="dto">请求的打卡地址信息</param>
        /// <returns></returns>
        public AttendanceLocationDto Create(AdminDto admin, AttendanceLocationRequestDto dto)
        {
            AttendanceLocationEntity entity = dto.As<AttendanceLocationEntity>();
            entity.AdminName = admin.FullName;
            entity.AdminId = admin.AdminId;

            return attendanceLocationData.InsertAsync(entity).GetAwaiter().GetResult().As<AttendanceLocationDto>();
        }

        /// <summary>
        ///删除考勤打卡地址信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="locationId">地址Id</param>
        /// <returns></returns>

        public int Delete(AdminDto admin, long locationId)
        {
            return attendanceLocationData.DeleteAsync(locationId, admin.FullName).GetAwaiter().GetResult();
        }

        /// <summary>
        /// 获取考勤打卡地址信息
        /// </summary>
        /// <param name="locationId">地址Id</param>
        /// <returns></returns>
        public AttendanceLocationDto Get(long locationId)
        {
            return attendanceLocationData.GetEntityAsync(locationId).GetAwaiter().GetResult().As<AttendanceLocationDto>();
        }

        /// <summary>
        /// 获取考勤打卡地址信息列表
        /// </summary>
        /// <returns></returns>
        public List<AttendanceLocationDto> GetList()
        {
            return attendanceLocationData.GetAsync().GetAwaiter().GetResult().As<List<AttendanceLocationDto>>();
        }


        /// <summary>
        /// 获取打卡地址
        /// </summary>
        /// <param name="groupId">考勤组Id</param>
        /// <returns></returns>
        public List<AttendanceLocationDto> GetListByModeIdAndGroupId(long groupId)
        {
            return attendanceLocationData.GetListByModeIdAndGroupIdAsync(groupId).GetAwaiter().GetResult().As<List<AttendanceLocationDto>>();
        }

        /// <summary>
        /// 修改指定的打卡地址信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="locationId">地址Id</param>
        /// <param name="dto">修改信息</param>
        /// <returns></returns>
        public AttendanceLocationDto Update(AdminDto admin, long locationId, AttendanceLocationRequestDto dto)
        {
            if (!attendanceLocationData.CheckIsExist(locationId).GetAwaiter().GetResult())
            {
                throw new ArgumentException("打卡地址信息不存在");
            }
            AttendanceLocationEntity entity = dto.As<AttendanceLocationEntity>();
            entity.ModifierId = admin.AdminId;
            entity.ModifierName = admin.FullName;
            entity.ModifyTime = DateTime.Now;
            return attendanceLocationData.UpdateAsync(entity).GetAwaiter().GetResult().As<AttendanceLocationDto>();
        }
    }
}
